﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ebada.Platform.Model;
using Ebada.UI.Base;
using Ebada.Client;
using Ebada.Core;
using Ebada.Client.Platform;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace Ebada.Modules.Vehicle
{
    /// <summary>
    /// 车辆参数登记编辑
    /// </summary>
    public partial class frmVehiclesDetailEdit : FormBase, IPopupFormEdit {
        SortableSearchableBindingList<mOrg> m_OrgID = new SortableSearchableBindingList<mOrg>();
        SortableSearchableBindingList<Empolyees> m_Employ= new SortableSearchableBindingList<Empolyees>();

        public frmVehiclesDetailEdit()
        {
            InitializeComponent();
        }
        private void frmVehiclesDetailEdit_Load(object sender, EventArgs e)
        {

            Add_FromDIC();

        }
        void dataBind() 
        {


            this.luVehicleID.DataBindings.Add("EditValue", rowData, "VehicleID");
            this.cobManufacturerName.DataBindings.Add("EditValue", rowData, "ManufacturerName");
            this.cobBrand.DataBindings.Add("EditValue", rowData, "Brand");
            this.cobColor.DataBindings.Add("EditValue", rowData, "Color");
           
            this.txtEngineType.DataBindings.Add("EditValue", rowData, "EngineType");
            this.txtDischarge.DataBindings.Add("EditValue", rowData, "Discharge");
            this.txtTireNum.DataBindings.Add("EditValue", rowData, "TireNum");
            this.txtAxleBase.DataBindings.Add("EditValue", rowData, "AxleBase");
            this.txtAxleNum.DataBindings.Add("EditValue", rowData, "AxleNum");
            this.txtInternalSize.DataBindings.Add("EditValue", rowData, "InternalSize");
            this.txtCapacityLoadWeight.DataBindings.Add("EditValue", rowData, "CapacityLoadWeight");
            this.txtRatingPassengers.DataBindings.Add("EditValue", rowData, "RatingPassengers");
            this.txtCabRidesPopulation.DataBindings.Add("EditValue", rowData, "CabRidesPopulation");
            this.txtVehicleModel.DataBindings.Add("EditValue", rowData, "VehicleModel");
            this.txtEngineNum.DataBindings.Add("EditValue", rowData, "EngineNum");
            this.cobFuelType.DataBindings.Add("EditValue", rowData, "FuelType");
            this.txtSteeringType.DataBindings.Add("EditValue", rowData, "SteeringType");
            this.txtTireSize.DataBindings.Add("EditValue", rowData, "TireSize");
            this.txtOverallSize.DataBindings.Add("EditValue", rowData, "OverallSize");
            this.txtQualityUtilizationFactor.DataBindings.Add("EditValue", rowData, "QualityUtilizationFactor");
            this.txtMaxSemiTrailerWeight.DataBindings.Add("EditValue", rowData, "MaxSemiTrailerWeight");
            this.daeLeaveFactoryDate.DataBindings.Add("EditValue", rowData, "LeaveFactoryDate");
            this.cobVehicleType.DataBindings.Add("EditValue", rowData, "VehicleType");
            this.txtChassisNum.DataBindings.Add("EditValue", rowData, "ChassisNum");
            if (rowData.IsImport)
            {
                cobIsImport.Text = "进口";
            }
            else
            {
                cobIsImport.Text = "国产";
            }
           
            this.txtWheelBase.DataBindings.Add("EditValue", rowData, "WheelBase");
            this.txtPlateSpringNum.DataBindings.Add("EditValue", rowData, "PlateSpringNum");
            this.txtTotalWeight.DataBindings.Add("EditValue", rowData, "TotalWeight");
            this.txtTowsWeight.DataBindings.Add("EditValue", rowData, "TowsWeight");
            this.txtDimensionaBarCode.DataBindings.Add("EditValue", rowData, "DimensionaBarCode");
            this.meRemark.DataBindings.Add("EditValue", rowData, "Remark");
        }
        Vehicle_Common vc = new Vehicle_Common();
        private void Add_FromDIC()
        {
           
            vc.Add_DIC("Color", cobColor);
            vc.Add_DIC("Brand", cobBrand);
           
            vc.Add_DIC("VehicleType", cobVehicleType);
            vc.Add_DIC("FuelType", cobFuelType);
            vc.Add_DIC("ManufacturerName", cobManufacturerName);
        }
       
        #region IPopupFormEdit Members
        private VehiclesDetail rowData = null;

        public object RowData {
            get {
                return rowData;
            }
            set {
                if (value == null) return;
                if (rowData == null) {
                    this.rowData = value as VehiclesDetail;
                    dataBind();
                    InitComboBoxData();
                } else {
                    ConvertHelper.CopyTo<VehiclesDetail>(value as VehiclesDetail, rowData);
                }
            }
        }

        #endregion

        private void InitComboBoxData() 
        {
          
            IList<Vehicles> vehi_list = MainHelper.PlatformSqlMap.GetList<Vehicles>("");
            this.SetComboBoxData3(this.luVehicleID, "VehicleNumber", "VehicleID", "请选择", "车牌号码", vehi_list);

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="comboBox"></param>
        /// <param name="displayMember"></param>
        /// <param name="valueMember"></param>
        /// <param name="nullTest"></param>
        /// <param name="cnStr"></param>
        /// <param name="post"></param>
       //单位选择器
        public void SetComboBoxData(DevExpress.XtraEditors.LookUpEdit comboBox, string displayMember, string valueMember, string nullTest, string cnStr, SortableSearchableBindingList<mOrg> post)
        {
            comboBox.Properties.Columns.Clear();
            comboBox.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            comboBox.Properties.DataSource = post;
            comboBox.Properties.DisplayMember = displayMember;
            comboBox.Properties.ValueMember = valueMember;
            comboBox.Properties.NullText = nullTest;
            comboBox.Properties.Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] {
            new DevExpress.XtraEditors.Controls.LookUpColumnInfo(valueMember, "ID", 20, DevExpress.Utils.FormatType.None, "", false, DevExpress.Utils.HorzAlignment.Default),
            new DevExpress.XtraEditors.Controls.LookUpColumnInfo(displayMember, cnStr)});
        }
        //人员选择器
        public void SetComboBoxData2(DevExpress.XtraEditors.LookUpEdit comboBox, string displayMember, string valueMember, string nullTest, string cnStr, SortableSearchableBindingList<Empolyees> post)
        {
            comboBox.Properties.Columns.Clear();
            comboBox.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            comboBox.Properties.DataSource = post;
            comboBox.Properties.DisplayMember = displayMember;
            comboBox.Properties.ValueMember = valueMember;
            comboBox.Properties.NullText = nullTest;
            comboBox.Properties.Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] {
            new DevExpress.XtraEditors.Controls.LookUpColumnInfo(valueMember, "ID", 20, DevExpress.Utils.FormatType.None, "", false, DevExpress.Utils.HorzAlignment.Default),
            new DevExpress.XtraEditors.Controls.LookUpColumnInfo(displayMember, cnStr)});
        }
        //车牌号选择器
        public void SetComboBoxData3(DevExpress.XtraEditors.LookUpEdit comboBox, string displayMember, string valueMember, string nullTest, string cnStr, IList<Vehicles> post)
        {
            comboBox.Properties.Columns.Clear();
            comboBox.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            comboBox.Properties.DataSource = post;
            comboBox.Properties.DisplayMember = displayMember;
            comboBox.Properties.ValueMember = valueMember;
            comboBox.Properties.NullText = nullTest;
            comboBox.Properties.Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] {
            new DevExpress.XtraEditors.Controls.LookUpColumnInfo(valueMember, "ID", 20, DevExpress.Utils.FormatType.None, "", false, DevExpress.Utils.HorzAlignment.Default),
            new DevExpress.XtraEditors.Controls.LookUpColumnInfo(displayMember, cnStr)});
        }
      

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (luVehicleID.Text.Length == 0)
            {
                MessageBox.Show("车牌号码不能为空！");
                return;
            }
            if (cobManufacturerName.Text.Length==0)
            {
                MessageBox.Show("制造厂名称不能为空！");
                return;
            }
            if (cobFuelType.Text.Length==0)
            {
                MessageBox.Show("燃料种类不能为空！");
                return;
            }
            IList<VehiclesDetail> objlist = MainHelper.PlatformSqlMap.GetListByWhere<VehiclesDetail>(" where VehicleID='" + luVehicleID.EditValue.ToString() + "'");
            if (objlist.Count > 0)
            {
                if (rowData.VehiclesDetailID != objlist[0].VehiclesDetailID)
                {
                    MessageBox.Show("此车牌已经存在。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            if (cobIsImport.Text=="进口")
            {
                rowData.IsImport = true;
            }
            else
            {
                rowData.IsImport = false;
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        //所属单位选择
        private void luOrgCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 1)
            {

                //frmOrgSelector2 frm = new frmOrgSelector2();
                //frm.IntionDataAll();
                //if (frm.ShowDialog() == DialogResult.OK)
                //{
                //    luOrgCode.Text = frm.SelectObj.cnName;
                //}

              

            }
        }
        //使用单位选择
        private void luUsingOrgCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 1)
            {
                //frmOrgSelector2 frm = new frmOrgSelector2();
                //frm.IntionDataAll();
                //if (frm.ShowDialog() == DialogResult.OK)
                //{
                //    luUsingOrgCode.Text = frm.SelectObj.cnName;

                //}
            }
        }
        //车主驾驶人中选择
        private void luEmployeeNo_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 1)
            {
                //Ebada.Client.Platform.Selector.frmEmpSelectorByOrgInfo frm = new Ebada.Client.Platform.Selector.frmEmpSelectorByOrgInfo();
                //frm.InitionData("", false, false);
                //if (frm.ShowDialog() == DialogResult.OK)
                //{
                //    luEmployeeNo.Text = frm.CurrentEmp.Empolyee_Name;
                //}
            }
        }
        private void luVehicleID_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index==1)
            {
                frmVehiclesSeletcter frm = new frmVehiclesSeletcter();
                frm.InitData_Slect("");
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    luVehicleID.EditValue = frm.SelectObj.VehicleID;
                }
            }
        }

      


      
    }
}